Je veux obtenir du texte à partir du Web, j'ai écrit du code, j'utilise indexOf et subString, mais cela ne fonctionne pas. int index = response_str.indexOf ("Adresse IP distante:"); index + = "Adresse IP distante:". length (); index + = "
". length ();
int end = response_str.indexOf ("
", index); String strIP = response_str.substring (index, fin); Log.d ("", "Bonjour" + strIP); Et ceci est un exemple de texte, je veux obtenir 49.156.53.152
Adresse IP distante:
** 49.156.53.152 **
UserAgent:
Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.11 (KHTML, comme Gecko) Chrome / 23.0.1271.64 Safari / 537.11
2020-12-21 08:44:11
Vous pouvez convertir la page html en une piqûre. et utilisez des opérations regex / string pour obtenir les données nécessaires essayez { if (! url_text.getText (). toString (). trim (). equalsIgnoreCase ("")) { textView.setText (""); Client HttpClient = nouveau DefaultHttpClient (); HttpGet request = new HttpGet (url_text.getText (). ToString ()); // Récupère la réponse ResponseHandlerresponseHandler = new BasicResponseHandler (); Chaîne response_str = client.execute (requête, responseHandler); textView.setText (response_str); }autre{ Toast.makeText (getApplicationContext (), "Chaîne URL vide.", Toast.LENGTH_LONG) .show (); } } catch (Exception e) { System.out.println ("Une erreur s'est produite."); textView.setText (e.getMessage ()); } peut-être que le moyen le plus simple est d'utiliser la fonction sting.split Chaîne [] séparé = response_str.split ("**"); séparé [0]; // partie avant le ** séparés [1]; // votre chaîne IP nécessaire séparés [2]; // partie après la seconde ** | Vous devez utiliser l'objet Interface Javascript avec Java pour récupérer toutes les données de la page Web Html. WebView final = (WebView) findViewById (R.id.browser); webview.getSettings (). setJavaScriptEnabled (true); webview.addJavascriptInterface (nouvelle MyJavaScriptInterface (this), "HtmlViewer"); webview.setWebViewClient (nouveau WebViewClient () { @Passer outre public void onPageFinished (vue WebView, chaîne url) { webview.loadUrl ("javascript: window.HtmlViewer.showHTML" + "('' + document.getElementsByTagName ('html') [0] .innerHTML + '');"); } }); webview.loadUrl ("http://android-in-action.com/index.php?post/" + "Erreurs-et-bogues-courants-et-comment-les-résoudre-les-éviter"); } class MyJavaScriptInterface { Contexte privé ctx; MyJavaScriptInterface (Contexte ctx) { this.ctx = ctx; } public void showHTML (String html) { nouveau AlertDialog.Builder (ctx) .setTitle ("HTML"). setMessage (html) .setPositiveButton (android.R.string.ok, null) .setCancelable (false) .create (). show (); } } | Vous devriez essayer d'utiliser ceci. myString = String.subString (" ", VOTRE_ORIGINAL_STRING.len); Chaîne requise = myString.endsWith (""); | Vous pouvez utiliser JSoup. Document doc = Jsoup.connect (URL_TO_HTML_PAGE) .get (); Éléments newsHeadlines = doc.getElementsByAttribute ("valeur1"); String ip = newsHeadlines [0] .text (). Split ("**") [1]; La dernière ligne est basée sur le post de Greezer. Personnellement, je le remplacerais par un simple motif regex qui correspond à toutes les adresses IP possibles. Quelque chose comme: \ b (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [ 0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01 ]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9] ?) \ b | Ta Réponse StackExchange.ifUsing ("éditeur", fonction () { StackExchange.using ("externalEditor", function () { StackExchange.using ("extraits", function () { StackExchange.snippets.init (); }); }); }, "extraits de code"); StackExchange.ready (fonction () { var channelOptions = { tags: "" .split (""), id: "1" }; initTagRenderer ("". split (""), "" .split (""), channelOptions); StackExchange.using ("externalEditor", function () { // Doit lancer l'éditeur après les extraits, si les extraits sont activés if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using ("extraits", function () { createEditor (); }); } autre { createEditor (); } }); function createEditor () { StackExchange.prepareEditor ({ useStacksEditor: faux, heartbeatType: 'réponse', autoActivateHeartbeat: faux, convertImagesToLinks: vrai, noModals: vrai, showLowRepImageUploadWarning: vrai, reputationToPostImages: 10, bindNavPrevention: vrai, suffixe: "", imageUploader: { brandingHtml: "Powered by \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "height = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46.2665 7.94324 47.1084 7.58816C47.4091 7.46349 47.7169 7.36433 48.0099 7.26993C48.9099 6.97997 49.672 6.73443 49.672 5.93063C49.672 5.22043 48.9832 4.61182 48.1414 4.61182C47.4335 4.61182 46.72554.9762.6943 4.61182C47.4335 4.61182 46.72554.91628 46.094 4.69.4335 4.61182 46.72554.91628 46.094.49.48.4335 4.61182 46.7256 4.9762.692 43.1481 6.59048V11.9512C43.1481 13.2535 43.6264 13.8962 44.6595 13.8962C45.6924 13.8962 46.1709 13.2535 46.1709 11.9512V9.17788Z \ "/ \ u003e \ u003cpath d = \" M32.492 10.1419C32.492 12.6954.0 34184.01448.0 14.6954.0 34184.0 34184.0 14.6954.0 34184.0 14484.0 14.6954.0 34184.0 14484.0 41,5985 12,6954 41,5985 10,1419V6,59049C41,5985 5,28821 41,1394 4,66232 40,1061 4,66232C39,0732 4,66232 38,5948 5,28821 38,5948 6,59049V9,60062C38,5948 10,8521 38,2696 11,5455 37,0451 11,4455,5C35,820 521 35.4954 9.60062V6.59049C35.4954 5.28821 35.0173 4.66232 34.0034 4.66232C32.9703 4.66232 32.492 5.28821 32.492 6.59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ "clip-rule = \" evenodd \ "d. = \ "M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30.2537 15.6379C30.8468 14.7755 30.9615 13.5579 30.9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C28.995 4.95 4.66231 29.4502 4.66231C28.995 4.95 4.66231 28.4502 4.66231C28.995Vous pouvez convertir la page html en une piqûre. et utilisez des opérations regex / string pour obtenir les données nécessaires essayez { if (! url_text.getText (). toString (). trim (). equalsIgnoreCase ("")) { textView.setText (""); Client HttpClient = nouveau DefaultHttpClient (); HttpGet request = new HttpGet (url_text.getText (). ToString ()); // Récupère la réponse ResponseHandlerresponseHandler = new BasicResponseHandler (); Chaîne response_str = client.execute (requête, responseHandler); textView.setText (response_str); }autre{ Toast.makeText (getApplicationContext (), "Chaîne URL vide.", Toast.LENGTH_LONG) .show (); } } catch (Exception e) { System.out.println ("Une erreur s'est produite."); textView.setText (e.getMessage ()); } peut-être que le moyen le plus simple est d'utiliser la fonction sting.split Chaîne [] séparé = response_str.split ("**"); séparé [0]; // partie avant le ** séparés [1]; // votre chaîne IP nécessaire séparés [2]; // partie après la seconde ** | Vous devez utiliser l'objet Interface Javascript avec Java pour récupérer toutes les données de la page Web Html. WebView final = (WebView) findViewById (R.id.browser); webview.getSettings (). setJavaScriptEnabled (true); webview.addJavascriptInterface (nouvelle MyJavaScriptInterface (this), "HtmlViewer"); webview.setWebViewClient (nouveau WebViewClient () { @Passer outre public void onPageFinished (vue WebView, chaîne url) { webview.loadUrl ("javascript: window.HtmlViewer.showHTML" + "('' + document.getElementsByTagName ('html') [0] .innerHTML + '');"); } }); webview.loadUrl ("http://android-in-action.com/index.php?post/" + "Erreurs-et-bogues-courants-et-comment-les-résoudre-les-éviter"); } class MyJavaScriptInterface { Contexte privé ctx; MyJavaScriptInterface (Contexte ctx) { this.ctx = ctx; } public void showHTML (String html) { nouveau AlertDialog.Builder (ctx) .setTitle ("HTML"). setMessage (html) .setPositiveButton (android.R.string.ok, null) .setCancelable (false) .create (). show (); } } | Vous devriez essayer d'utiliser ceci. myString = String.subString (" ", VOTRE_ORIGINAL_STRING.len); Chaîne requise = myString.endsWith (""); | Vous pouvez utiliser JSoup. Document doc = Jsoup.connect (URL_TO_HTML_PAGE) .get (); Éléments newsHeadlines = doc.getElementsByAttribute ("valeur1"); String ip = newsHeadlines [0] .text (). Split ("**") [1]; La dernière ligne est basée sur le post de Greezer. Personnellement, je le remplacerais par un simple motif regex qui correspond à toutes les adresses IP possibles. Quelque chose comme: \ b (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [ 0-4] [0-9] | [01]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01 ]? [0-9] [0-9]?) \. (25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9] ?) \ b | Ta Réponse StackExchange.ifUsing ("éditeur", fonction () { StackExchange.using ("externalEditor", function () { StackExchange.using ("extraits", function () { StackExchange.snippets.init (); }); }); }, "extraits de code"); StackExchange.ready (fonction () { var channelOptions = { tags: "" .split (""), id: "1" }; initTagRenderer ("". split (""), "" .split (""), channelOptions); StackExchange.using ("externalEditor", function () { // Doit lancer l'éditeur après les extraits, si les extraits sont activés if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using ("extraits", function () { createEditor (); }); } autre { createEditor (); } }); function createEditor () { StackExchange.prepareEditor ({ useStacksEditor: faux, heartbeatType: 'réponse', autoActivateHeartbeat: faux, convertImagesToLinks: vrai, noModals: vrai, showLowRepImageUploadWarning: vrai, reputationToPostImages: 10, bindNavPrevention: vrai, suffixe: "", imageUploader: { brandingHtml: "Powered by \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "height = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46.2665 7.94324 47.1084 7.58816C47.4091 7.46349 47.7169 7.36433 48.0099 7.26993C48.9099 6.97997 49.672 6.73443 49.672 5.93063C49.672 5.22043 48.9832 4.61182 48.1414 4.61182C47.4335 4.61182 46.72554.9762.6943 4.61182C47.4335 4.61182 46.72554.91628 46.094 4.69.4335 4.61182 46.72554.91628 46.094.49.48.4335 4.61182 46.7256 4.9762.692 43.1481 6.59048V11.9512C43.1481 13.2535 43.6264 13.8962 44.6595 13.8962C45.6924 13.8962 46.1709 13.2535 46.1709 11.9512V9.17788Z \ "/ \ u003e \ u003cpath d = \" M32.492 10.1419C32.492 12.6954.0 34184.01448.0 14.6954.0 34184.0 34184.0 14.6954.0 34184.0 14484.0 14.6954.0 34184.0 14484.0 41,5985 12,6954 41,5985 10,1419V6,59049C41,5985 5,28821 41,1394 4,66232 40,1061 4,66232C39,0732 4,66232 38,5948 5,28821 38,5948 6,59049V9,60062C38,5948 10,8521 38,2696 11,5455 37,0451 11,4455,5C35,820 521 35.4954 9.60062V6.59049C35.4954 5.28821 35.0173 4.66232 34.0034 4.66232C32.9703 4.66232 32.492 5.28821 32.492 6.59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ "clip-rule = \" evenodd \ "d. = \ "M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30.2537 15.6379C30.8468 14.7755 30.9615 13.5579 30.9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C28.995 4.95 4.66231 29.4502 4.66231C28.995 4.95 4.66231 28.4502 4.66231C28.995